package com.zmn.oms.task.job.clue;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.zmn.business.interfaces.clue.OrderClueBService;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 类描述 给线索系统同步信息Job
 *
 * @author fubiao
 * @since 2022/09/19
 */
@Slf4j
@LTS
public class OrderWorkClueJob {

    @Resource
    private TaskManager taskManager;

    @Resource
    private OrderClueBService orderClueBService;

    @PostConstruct
    public void init() {
        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_ORDER_SYNC_CLUE_JOB);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_ORDER_SYNC_CLUE_JOB);
        job.setCronExpression("0 0 3 * * ?");
        job.setReplaceOnExist(true);
        ResponseDTO<String> resp = taskManager.submitTask2(job);
        if (!resp.isSuccess()) {
            log.error("#oms# OrderWorkClueJob job submit fail...");
        }
    }

    /**
     * 每天凌晨三点执行
     *
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_ORDER_SYNC_CLUE_JOB)
    public Result syncOrderWorkToClue(Job job) {
        log.info("#oms#syncOrderWorkToClue 开始同步工单数据到线索系统 ");

        try {
            orderClueBService.sendOrderOfCronJobToClue();
        } catch (Exception ex) {
            log.error("#oms#syncOrderWorkToClue 同步工单数据到线索系统异常", ex);
            return new Result(Action.EXECUTE_FAILED, "同步工单数据到线索系统执行异常!");
        }
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
